<?php
/**
 * YANA library
 *
 * Software:  Yana PHP-Framework
 * Version:   {VERSION} - {DATE}
 * License:   GNU GPL  http://www.gnu.org/licenses/
 *
 * This program: can be redistributed and/or modified under the
 * terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 *
 * This notice MAY NOT be removed.
 *
 * @package  yana
 * @license  http://www.gnu.org/licenses/gpl.txt
 */

namespace Plugins\SDK;

/**
 * Plugin configurator for creating manual plugin methods
 *
 * @package    yana
 * @subpackage plugins
 */
class MethodConfiguration extends \Yana\Plugins\Configs\MethodConfiguration
{

    /**
     * is auto-generated method
     *
     * true = yes, false = no
     *
     * @var  bool
     */
    private $_isAutoGenerated = false;

    /**
     * set auto-generated
     *
     * Set to bool(true) if the method was created automatically by the
     * CRUD-page generator.
     *
     * @param   bool  $isAutoGenerated  true = yes (default), false = no
     * @return  \Plugins\SDK\MethodConfiguration 
     */
    public function setAutoGenerated($isAutoGenerated = true)
    {
        assert('is_bool($isAutoGenerated); // Wrong argument type argument 1. Boolean expected');
        $this->_isAutoGenerated = (bool) $isAutoGenerated;
        return $this;
    }

    /**
     * is auto-generated method
     *
     * Return bool(true) if the method was created automatically by the
     * CRUD-page generator and bool(false) otherwise.
     *
     * @return  bool
     */
    public function isAutoGenerated()
    {
        return $this->_isAutoGenerated === true;
    }

    /**
     * Outputs the method head, including doc-comment.
     *
     * @return  string
     */
    public function __toString()
    {
        $tab = "\n     * ";
        $string = "    /**" . $tab;
        // head line
        if ($this->getTitle()) {
            $string .= $this->getTitle();
        } else {
            $string .= $this->getMethodName();
        }
        $string .= $tab; // empty line after title
        // annotations
        if ($this->getSubscribe()) {
            $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::SUBSCRIBE;
        } else {
            if ($this->getOverwrite()) {
                $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::OVERWRITE;
            }
            if ($this->getType()) {
                $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::TYPE . "       " . $this->getType();
            }
            if ($this->getTemplate()) {
                $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::TEMPLATE . "   " . $this->getTemplate();
            }
            foreach ($this->getStyles() as $item)
            {
                $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::STYLE . "      " . $item;
            }
            unset($item);
            foreach ($this->getScripts() as $item)
            {
                $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::SCRIPT . "     " . $item;
            }
            unset($item);
            foreach ($this->getLanguages() as $item)
            {
                $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::LANGUAGE . "   " . $item;
            }
            unset($item);
            if ($this->getMenu()) {
                $item = $this->getMenu();
                if ($item->getGroup()) {
                    $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::MENU . "       " .
                        \Yana\Plugins\Annotations\Enumeration::GROUP . ": " . $item->getGroup() .
                        (($item->getTitle()) ? ", " .
                        \Yana\Plugins\Annotations\Enumeration::TITLE .  ": ". $item->getTitle() : "");
                }
            }
            assert('!isset($settings); // Cannot redeclare var $settings');
            foreach ($this->getUserLevels() as $item)
            {
                /* @var $item PluginUserLevel */
                $settings = array();
                if ($item->getGroup()) {
                    $settings[] = \Yana\Plugins\Annotations\Enumeration::GROUP . ": " . $item->getGroup();
                }
                if ($item->getRole()) {
                    $settings[] = \Yana\Plugins\Annotations\Enumeration::ROLE . ": " . $item->getRole();
                }
                if ($item->getLevel()) {
                    $settings[] = \Yana\Plugins\Annotations\Enumeration::LEVEL . ": " . $item->getLevel();
                }
                if (!empty($settings)) {
                    $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::USER . "       "  . implode(', ', $settings);
                }
            }
            unset($item, $settings);
            if ($this->getOnError()) {
                $item = $this->getOnError();
                if ($item->getTarget()) {
                    $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::ONERROR . "    " .
                        \Yana\Plugins\Annotations\Enumeration::GO . ": " . $item->getTarget() .
                        (($item->getMessage()) ? ", " .
                        \Yana\Plugins\Annotations\Enumeration::TEXT .  ": ". $item->getType() : "");
                }
            }
            if ($this->getOnSuccess()) {
                $item = $this->getOnSuccess();
                if ($item->getTarget()) {
                    $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::ONSUCCESS . "  " .
                        \Yana\Plugins\Annotations\Enumeration::GO . ": " . $item->getTarget() .
                        (($item->getMessage()) ? ", " .
                        \Yana\Plugins\Annotations\Enumeration::TEXT .  ": ". $item->getType() : "");
                }
            }
            if ($this->getSafeMode()) {
                $string .= $tab . "@" . \Yana\Plugins\Annotations\Enumeration::SAFEMODE . "    " . $this->getSafeMode();
            }
        }
        $string .= $tab . "@access   public";
        foreach ($this->getParams() as $name => $type)
        {
            $string .= $tab . "@param    " . $type . "  \$" . $name . "  " . $name;
        }
        if ($this->getReturn()) {
            $string .= $tab . "@return   " . $this->getReturn();
        }
        $string .= "\n     */\n    public function " . $this->getMethodName() . "(";
        $params = $this->getParams();
        while (current($params) !== false)
        {
            if (key($params) === 'array') {
                $string .= "array ";
            }
            $string .= '$' . current($params);
            if (next($params) !== false) {
                $string .= ", ";
            }
        }
        $string .= ")";
        return $string;
    }
}

?>